/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.mojo.flex.generator;

import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;


/**
 * @author Izzet_Mustafayev
 *
 */
public class ClassesResolver {
    /**
     * File to generate as3 file. If not defined assumes all classes must be
     * included.
     */
    private String[] includeJavaClasses;

    /**
     * File to exclude from as3 generation. If not defined, assumes no
     * exclusions.
     */
    private String[] excludeJavaClasses;

    public ClassesResolver(final String[] included, final String[] excluded) {
        includeJavaClasses = included;
        excludeJavaClasses = excluded;
    }

    public Map<String, File> resolveClasses(List<File> jarDependencies)
        throws IOException {
        Map<String, File> classes = new HashMap<String, File>();

        for (File file : jarDependencies) {
            JarInputStream jar = new JarInputStream(new FileInputStream(file));

            JarEntry jarEntry;

            while (true) {
                jarEntry = jar.getNextJarEntry();

                if (jarEntry == null) {
                    break;
                }

                String className = jarEntry.getName();

                if (jarEntry.isDirectory() || !className.endsWith(".class")) {
                    continue;
                }

                className = className.replace('/', '.');
                className = className.substring(0, className.length() - 6);

                if (matchWildCard(className, includeJavaClasses) && !matchWildCard(className, excludeJavaClasses)) {
                    classes.put(className, file);
                }
            }
        }

        return classes;
    }

    private boolean matchWildCard(String className, String[] wildCards) {
        if (wildCards == null) {
            return false;
        }

        for (String wildCard : wildCards) {
            if (FilenameUtils.wildcardMatch(className, wildCard)) {
                return true;
            }
        }

        return false;
    }
}
